package com.example.pc.listview05;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 2017/9/15.
 */


public class MyAdapter extends BaseAdapter {


    private Context context;
    private List<String> item_1 = new ArrayList<>();

    public MyAdapter(Context context, List<String>item_1){
        this.context= context;
        this.item_1 =item_1;
    }

    @Override
    public int getCount() {
        return item_1.size();
    }

    @Override
    public Object getItem(int position) {
        return item_1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return item_1.size();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         if (convertView==null){
             convertView = LayoutInflater.from(context).inflate(R.layout.item_1, null);
         }

        final TextView textView1 =(TextView)convertView.findViewById(R.id.textView1);
        Button button1 =(Button)convertView.findViewById(R.id.button1);


        final String item = item_1.get(position);
        textView1.setText("第"+ item + "项");
        convertView.setTag(position);

        button1.setOnClickListener(new View.OnClickListener() {        //设置button监听事件实现intent跳转
            @Override
            public void onClick(View v) {
                String str1 = textView1.getText().toString();//获取TextView的内容
                Bundle bundle = new Bundle();//创建一个bundle对象，用来传递当前活动到下一个活动的信息
                bundle.putString("key",str1);//将信息储存到bundle的key中
                Intent intent = new Intent(context, SecondActivity.class);
                intent.putExtras(bundle);//将bundle对象的信息储存到intent中
                context.startActivity(intent);//通过对象跳转
            }
        });


        return convertView;
    }


    public void setItem_1(List<String> item) {
        this.item_1 = item;
        notifyDataSetChanged();
    }

    public void addItem(String item) {
        this.item_1.add(item);
        notifyDataSetChanged();
    }
    public List<String >getItem_1(){
    return item_1;
}

    public void deleteItem() {
        this.item_1.remove(item_1.size() - 1);
        notifyDataSetChanged();
    }

    public int deleteItemInListView(int position){
        this.item_1.remove(position);
        notifyDataSetChanged();
        return position;
    }

}
